<template>
  <div class="storeManagement">
    <div class="operate">
      <el-button type="primary" @click="newItem">新增</el-button>
    </div>
    <el-table
      :data="tableData"
      style="width: 100%">
      <el-table-column
        prop="shopId"
        label="店铺编号">
      </el-table-column>
      <el-table-column
        prop="shopName"
        label="店铺名称">
      </el-table-column>
      <el-table-column
        prop="description"
        label="店铺描述">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="editItem(scope.row)">编辑</el-button>
          <el-button type="text" size="small" @click="deleteItem(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      :title="shopId ? '编辑店铺' : '新增店铺'"
      :visible.sync="dialogVisible"
      width="30%">
      <el-form
        ref="form"
        :model="shopForm"
        :rules="shopRules"
        label-width="80px">
        <el-form-item label="店铺编号" prop="shopId">
          <el-input v-model="shopForm.shopId"></el-input>
        </el-form-item>
        <el-form-item label="店铺名称" prop="shopName">
          <el-input v-model="shopForm.shopName"></el-input>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input type="textarea" v-model="shopForm.description"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelForm">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { getStoreManagement, newStoreManagement, deleteStoreManagement, checkStoreManagement, editStoreManagement } from "@/api/order";
export default {
  data() {
    return {
      pageNum: 1,
      pageSize: 10,
      total: 0,
      tableData: [],

      shopId: "",
      dialogVisible: false,
      shopForm: {
        shopId: "",
        shopName: "",
        description: "",
      },
      shopRules: {
        shopId: [
          { required: true, message: '请输入店铺编号', trigger: 'blur' },
        ],
        shopName: [
          { required: true, message: '请输入店铺名称', trigger: 'blur' },
        ],
        description: [
          { required: true, message: '请输入店铺描述', trigger: 'blur' },
        ]
      }
    }
  },
  created() {
    this.initTable();
  },
  methods: {
    initTable() {
      const params = {
        pageNum: this.pageNum,
        pageSize: this.pageSize
      }
      getStoreManagement(params).then(res => {
        const { total, rows } = res;
        this.tableData = rows;
        this.total = total;
      })
    },
    newItem() {
      this.shopForm = {shopId: "", shopName: "", description: ""};
      this.dialogVisible = true;
      this.shopId = "";
    },
    editItem(data) {
      const { id } = data;
      checkStoreManagement(id).then(res => {
        const { id, shopId, shopName, description } = res.data;
        this.shopForm = {shopId, shopName, description};
        this.dialogVisible = true;
        this.shopId = id;
      })
    },
    cancelForm() {
      this.$refs["form"].resetFields();
      this.dialogVisible = false;
    },
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if(this.shopId) {
            const params = { ...this.shopForm, id: this.shopId }
            editStoreManagement(params).then(res => {
              this.cancelForm();
              this.initTable();
              this.$message({
                type: 'success',
                message: '修改成功!'
              });
            })
          }else {
            const params = this.shopForm;
            newStoreManagement(params).then(res => {
              this.cancelForm();
              this.initTable();
              this.$message({
                type: 'success',
                message: '新增成功!'
              });
            })
          }
        }
      });
    },
    deleteItem(data) {
      this.$confirm('确定删除该店铺?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const { id } = data;
        deleteStoreManagement(id).then(res => {
          this.initTable();
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    }
  }
}
</script>
<style lang="scss" scoped>
div.storeManagement {
  div.operate {
    height: 36px;
    margin-bottom: 10px;
    display: flex;
    justify-content: end;
    align-items: center;
  }
}
</style>
